<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets" >
<ui:composition template="/templates/principal.xhtml" >
<ui:define name="corpo" >
<h:head>
    <h:outputScript library="js" name="tratardados.js" />
</h:head>
<h:body>
<p:growl id="mensagens" globalOnly="true" />
<p:panel header="Setores" style="text-align: center; height: 35px; padding: 0px; font-size: 16px"/> 
<p:layout style="width: auto; height: 960px;"  >  
    <p:layoutUnit position="center" scrollable="true">
    <h:form id="tabelaSetor" prependId="false">
        <h:panelGroup> 
            <h:outputLabel value="Unidade: " style="font-weight: bold" />
            <h:selectOneMenu id="porOM" value="#{setorBean.unidadeSelecionada}" >
                <f:selectItem itemValue="-1" itemLabel="Selecione" />
                <f:selectItems value="#{unidadeMilitarBean.listarTipos}" var="item" itemLabel="#{item.key}" itemValue="#{item.value}"/> 
                <!-- <f:ajax execute="@this" render="tabelaSetor" event="change" immediate="true" /> -->
                <f:ajax listener="#{setorBean.buscar()}" render="tabelaSetor" event="change" immediate="true" />
            </h:selectOneMenu>
            </h:panelGroup>
        <p:dataTable var="setor" value="#{setorBean.listarSetorPorOm}" emptyMessage="Não há informações!" selectionMode="single" >
            <h:inputHidden value="#{setor.idunidade}" />
            <h:inputHidden value="#{setor.idsetor}" />
            <p:columnGroup type="header">  
                <p:row>  
                    <p:column colspan="2" style="width: 15%">
                        <f:facet name="header">
                            <center>
                                <p:menubar autoSubmenuDisplay="true" style="width: 78px; height: 28px; padding: 0px;" >
                                    <p:submenu label="Incluir" icon="ui-icon ui-icon-document" > 
                                        <p:menuitem value="Novo setor" actionListener="#{setorBean.prepararAdicionarSetor(actionEvent)}" update="incluirSetor, tabelaSetor" oncomplete="incluirDialog.show()" icon="ui-icon ui-icon-document" />
                                    </p:submenu>
                                </p:menubar>
                            </center>
                        </f:facet>
                    </p:column>
                    <p:column headerText="Sigla" filterBy="#{setor.siglaSetor}" style="width: 15%"/> 
                    <p:column headerText="Descrição" /> 
                </p:row> 
            </p:columnGroup>
            <p:column style="width: 7%" >
                <center>
                    <p:commandLink title="Editar" update="editarSetor, incluirSetor" oncomplete="editarDialog.show()" >
                        <h:graphicImage library="imagens" name="editar16.png" style="border:0"/>
                        <f:setPropertyActionListener target="#{setorBean.setor}" value="#{setor}" />
                    </p:commandLink>
                </center>
            </p:column>
            <p:column style="width: 7%" >
                <center>
                    <p:commandLink action="#{setorBean.excluir}" update="tabelaSetor, mensagens" title="Excluir" onclick="if (!confirm('Confirma excluir o Setor #{setor.siglaSetor}?')) return false;"  rendered="#{setor.idsetor ne 1}" >
                        <h:graphicImage library="imagens" name="excluir16.png" style="border:0"/>
                        <f:setPropertyActionListener target="#{setorBean.setor}" value="#{setor}"/>
                    </p:commandLink>
                </center>
            </p:column>
            <p:column>
                <center>
                    <h:outputText value="#{setor.siglaSetor}" />
                </center>
            </p:column>
            <p:column>
                <center>
                    <h:outputText value="#{setor.descricaoSetor}" />
                </center>
            </p:column>
        </p:dataTable>
    </h:form>
    </p:layoutUnit>
</p:layout>
</h:body>

<p:dialog header="Incluir" widgetVar="incluirDialog" resizable="false" modal="true" width="500" showEffect="clip" hideEffect="clip"   >  
    <h:form prependId="false" id="incluirSetor">
        <h:panelGroup>
            <h:messages style="color: red" />
            <h:panelGrid  columns="2" cellspacing="2" cellpadding="4"  >
                <h:outputLabel value="Sigla:" for="sigla" />
                <h:inputText id="sigla" value="#{setorBean.setor.siglaSetor}" size="15" maxlength="15" required="true" requiredMessage="Campo Sigla obrigatório!" />
                <h:outputLabel value="Descrição:" for="st" />
                <h:inputText id="st" value="#{setorBean.setor.descricaoSetor}" size="60" maxlength="255" required="true" requiredMessage="Campo Descrição obrigatório!" />
            </h:panelGrid>
            <h:panelGroup>
                <center>
                    <p:commandButton update="editarSetor, incluirSetor, tabelaSetor, mensagens" oncomplete="handleComplete(xhr, status, args)" action="#{setorBean.salvar}" value="Incluir" />
                    <p:commandButton action="incluirDialog.hide();" oncomplete="incluirDialog.hide();" value="Fechar"/>
                </center>
            </h:panelGroup>
        </h:panelGroup>
    </h:form>
</p:dialog>
    
<p:dialog header="Editar" widgetVar="editarDialog" resizable="false" modal="true" width="500" showEffect="clip" hideEffect="clip" draggable="false"  >  
    <h:form id="editarSetor" prependId="false" >
        <h:inputHidden value="#{setorBean.setor.idsetor}" />
        <h:inputHidden value="#{setorBean.setor.idunidade}" />
        <h:panelGroup id="mensagemTipo">
            <h:messages style="color: red" />
        </h:panelGroup>
        <h:panelGrid  columns="2" cellspacing="2" cellpadding="2" >
            <h:outputLabel value="Sigla:" for="siglaE" />
            <h:inputText id="siglaE" value="#{setorBean.setor.siglaSetor}" size="15" maxlength="15" required="true" requiredMessage="Campo Sigla obrigatório!" />
            <h:outputLabel value="Descrição:" for="setorE" />
            <h:inputText id="setorE" value="#{setorBean.setor.descricaoSetor}" size="60" maxlength="255" required="true" requiredMessage="Campo Descrição obrigatório!" />
        </h:panelGrid>
        <br />
        <h:panelGroup>
            <center>
                <p:commandButton update="mensagemTipo, tabelaSetor, mensagens" oncomplete="handleComplete2(xhr, status, args)" action="#{setorBean.salvar}" value="Salvar" />
                <p:commandButton oncomplete="editarDialog.hide();" action="editarDialog.hide();" value="Fechar"/>
            </center>
        </h:panelGroup>
    </h:form>
</p:dialog>
</ui:define>
</ui:composition>
</html>
